<?php


use App\Library\Utils\Arr;
use Phinx\Seed\AbstractSeed;
use Faker\Factory as FakerFactory;

class NotificationSeeder extends AbstractSeed
{
    public function getDependencies(): array
    {
        return [
            'UserSeeder',
            'AnswerSeeder',
            'QuestionSeeder',
        ];
    }

    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * https://book.cakephp.org/phinx/0/en/seeding.html
     */
    public function run()
    {
        $faker = FakerFactory::create('zh_CN');

        $answers = $this->fetchAll('SELECT * FROM kg_answer');

        $rows = [];
        for ($i = 0; $i < 10; $i++) {
            $answer = Arr::rand($answers);
            $question = $this->fetchRow('SELECT * FROM kg_question WHERE id='.$answer['question_id']);
            $sender = $this->fetchRow('SELECT * FROM kg_user WHERE id='.$answer['owner_id']);
            $receiver = $this->fetchRow('SELECT * FROM kg_user WHERE id='.$question['owner_id']);

            $eventInfo = [
                'question' => [
                    'id' => $question['id'],
                    'title' => $question['title'],
                ],
                'answer' => [
                    'id' => $answer['id'],
                    'summary' => $answer['summary'],
                ],
            ];

            $rows[] = [
                'sender_id' => $sender['id'],
                'receiver_id' => $receiver['id'],
                'event_id' => rand(50, 200),
                'event_type' => 208,
                'event_info' => json_encode($eventInfo, JSON_UNESCAPED_UNICODE),
                'viewed' => rand(0, 1),
                'create_time' => $faker->unixTime,
                'update_time' => $faker->unixTime,
            ];
        }
        $this->table('kg_notification')->insert($rows)->saveData();
    }
}
